﻿/**
 * 
 * Copyright 2009 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using moose.irc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace moose.irc.standard
{
    [CLSCompliant(true)]
    [Command(
        name = "Connect Command",
        description = "Provides a means for the client to request the engine to connect to an irc server",
        author = "Mike MacKenzie-Jamer <email: mikejamer@gmail.com>",
        version = "0.1")]
    [Trigger(TriggerText="server")]
    [Trigger(TriggerText = "connect")]
    public sealed class ConnectCommand : IrcAction
    {
        protected override IrcState valid_states()
        {
            return IrcState.Disconnected | IrcState.None;
        }

        protected override void action(IrcClient client, IrcMessage input)
        {
            int port = 0;
            if (input.Arguments.Count < 2)
            {
                client.echo("Connect expecting at least 2 arguments, host/port...");
                return;
            }

            if (!int.TryParse(input.Arguments[1], out port))
            {
                client.echo("Port must be numeric");
                return;
            }

            if (port < 1 || port > 65535)
            {
                client.echo("port must be between 1 and 65535");
                return;
            }

            try
            {
                client.echo("Connecting to: " + input.Arguments[0] + ":" + input.Arguments[1]);
                client.connect(input.Arguments[0], port);

                //TODO: Consider sticking state updates in here

            }
            catch (Exception)
            {
                client.echo("Failed to connect to: " + input.Arguments[0] + ":" + input.Arguments[1]);
                return;
            }

            return;
        }
    }
}
